package middlewere

import (
	"github.com/gin-gonic/gin"
	"video_-chat/global"
)

func Logined() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.Request.Header.Get("token")
		if token == "" {
			//c.AbortWithStatusJSON(200, gin.H{
			//	"code": 100201,
			//	"msg":  "未登录",
			//})
			//return
			token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTI1ODE4MjEsImlhdCI6MTc1MjU3NDYyMSwidXNlciI6IntcInVzZXJfaWRcIjoyLFwibmlja25hbWVcIjpcIua1i-ivleeUqOaItzJcIixcImF2YXRhcl91cmxcIjpcIuaXoOWktOWDj1wiLFwicmVnaW9uXCI6XCLkuIrmtbdcIixcImludHJvZHVjdGlvblwiOlwi5peg566A5LuLXCIsXCJyZWdpc3Rlcl90aW1lXCI6XCIyMDI1LTA3LTE1VDEyOjE5OjAzKzA4OjAwXCIsXCJsb3N0X2xvZ2luX3RpbWVcIjpcIjIwMjUtMDctMTVUMTg6MTc6MDAuOTIxMjQxMiswODowMFwiLFwicGhvbmVcIjpcIjEzOTcyMjEzNzAzXCJ9In0.CShrFUVWtzM6dHnoeqBukAfJmj5kwOOy5ytGDqxn7MI"
		}
		user, _ := global.RDB.Get(global.Ctx, token).Result()
		if user == "" {
			c.AbortWithStatusJSON(200, gin.H{
				"code": 100202,
				"msg":  "登录过期",
			})
			return
		}
		c.Request.ParseForm()
		c.Request.Form.Set("user", user)
		c.Next()
	}
}
